package com.wq.code;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Author: wangqiang20995
 * @Date:2019/5/15
 * @Description:
 * @Resource:
 */
public class ReflectAPI {

    private static class InnerBean{
        private boolean initialized = false;

        public InnerBean(){
            System.out.println("initialized:" + initialized);
        }

        @Override
        public String toString() {
            System.out.println("call toString");
            return "InnerBean{" +
                    "initialized=" + initialized +
                    '}';
        }
    }
    private static class Father{
        public void showFather(){
            System.out.println("I am father");
        }

        private void showFatherOwnInfo(){
            System.out.println("father private method");
        }

        protected void showExtendedInfo(){
            System.out.println("father something can be extended");
        }
    }

    private static class Son extends Father{
        public void showSon(){
            System.out.println("I am son");
        }

        @Override
        protected void showExtendedInfo() {
            System.out.println("something extending from father");
        }

        private void showSonOwnInfo(){
            System.out.println("son private method");
        }

        @Override
        public String toString(){
            System.out.println("son");
            return "son";
        }
    }

    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
        Class<Son> clazz = Son.class;

        /**
         * getMethod:返回当前Class包含所有继承的Public方法
         */
        for(Method method : clazz.getMethods()){
            if("wait".equals(method.getName()) || "notify".equals(method.getName()) || "toString".equals(method.getName())
                    || "equals".equals(method.getName()) || "hashCode".equals(method.getName())
                    || "notifyAll".equals(method.getName()) || "getClass".equals(method.getName())){
                continue;
            }
            System.out.println("Method:" + method.getName());
        }

        /**
         * getDeclaredMethods:返回当前Class的所有声明的方法，不包含继承的
         */
        for(Method method : clazz.getDeclaredMethods()){
            System.out.println("DeclareMethods:" + method.getName());
            method.setAccessible(true);
            System.out.println("Call Method");
            method.invoke(new Son());
        }

        InnerBean innerBean = new InnerBean();
        System.out.println("123");
    }

}